/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Net.Client;
import java.io.BufferedInputStream;
   import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Administrator
 */
public class Client {
 


    private ClientSocket cs = null;

    private String ip = "localhost";// 设置成服务器IP

    private int port = 8821;

    private String sendMessage = "Windwos";
    public boolean isDone =false;
    public Client() {
        try {
            createConnection();
               
               
              

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
  
    private boolean createConnection() {
        cs = new ClientSocket(ip, port);
        try {
            cs.CreateConnection();
            System.out.print("连接服务器成功!" + "\n");
            return true;
        } catch (Exception e) {
            System.out.print("连接服务器失败!" + "\n");
            return false;
        }

    }
    public void sendMessage(){
        // 传消息任务
         DataOutputStream ps =cs.out;
         DataInputStream in =cs.inputStream;
   try {System.out.println("1");
    cs.write("CCD");  
   } catch (Exception e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
   }
   // 接收服务器发过的的确认消息bound，然后发送范围文件
   while (true) {
    try {
     if (cs.read().startsWith("BOUND")) {
      System.out.println("from server:BOUND");
      cs.UpLoad("E:\\text.txt");
     /* cs.UpLoad("BookList.txt");
      cs.UpLoad("CustomerList.txt");
      cs.UpLoad("CategoryList.txt");
      cs.UpLoad("DiscountCoupon.txt");
      cs.UpLoad("EquivalentCoupons.txt");
      cs.UpLoad("EquivalentCoupons.txt");*/
      isDone=true ;
      break;
     }
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     break;
    }
   }
   
    }
    public void getMessage(){
        try {  cs.write("downLoad");
   System.out.println("更新");
   } catch (Exception e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
   }
   while(true){
            try {   
                if(cs.read().startsWith("end"))
                   cs.downLoad();
                
                break ;
            } catch (IOException ex) {
                 ex.printStackTrace();
                  break;
            }
}
    }
    public void close() throws IOException{
        cs.close();
    }
   public int getPercent(){
       return cs.getPercent();
   }
/*class ClientSoc extends Thread{
      
       
        public void run(){
            try {this.sleep(1);
                 sendMessage("E:\\text.txt");
            } catch (Exception ex) {
                
            }
        }
    }*/
   
    /**
     *
     * @param arg
     * @throws Exception
     */
//    public static void main(String arg[]) throws Exception {
//     //  Client c =
//               new Client().getMessage();//.sendMessage("E:\\text.txt");
//              //c. sendMessage();  c.getMessage();
//               new Client().sendMessage();
//    }
}

